package com.gs.weather.dao;

import com.gs.weather.dao.mapper.DisasterTypeMapper;
import com.gs.weather.entity.DisasterType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.gs.weather.utils.Constant.PAGE_SIZE;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/9/21 0021
 */
@Repository
public class DisasterTypeDao {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    DisasterTypeMapper mapper;

    //添加灾害类型
    public Integer addDisasterType(DisasterType disasterType) {
        try {
            mapper.addDisasterType(disasterType);
            return disasterType.getId();
        } catch (Exception e) {
            logger.info("addDisasterType失败->{}", e.getMessage());
            return null;
        }
    }

    //更新灾害类型
    public void updateDisasterType(DisasterType disasterType) {
        try {
            mapper.updateDisasterType(disasterType);
        } catch (Exception e) {
            logger.info("updateDisasterType->{}", e.getMessage());
        }
    }

    //查询所有灾害类型
    public List<DisasterType> queryAllDisasterType(Integer pageNum) {
        try {
            List<DisasterType> disasterTypes = mapper.queryAllDisasterType((pageNum-1)*PAGE_SIZE,PAGE_SIZE);
            return disasterTypes;
        } catch (Exception e) {
            logger.info("queryAllDisasterType失败->{}", e.getMessage());
            return null;
        }
    }

    public int getTypeCount(){
        try {
            return mapper.queryTypeCount();
        }
        catch (Exception e){
            logger.info("queryTypeCount->{}",e.getMessage());
            return 0;
        }
    }


    //删除灾害类型
    public void deleteDisasterType(int id) {
        try {
            mapper.deleteDisasterType(id);
        } catch (Exception e) {
            logger.info("deleteDisasterType失败->{}", e.getMessage());
        }
    }

    //根据id删除所有灾害类型的子类型
    void deleteDisasterSubTypeByDisasterTypeId(int id) {

    }
}
